/**
 * 
 */
package com.whales.shared.util;

import java.net.*;
import java.security.*;
import java.io.*;


/**
 * IP   : 프로젝터 IP
 * Port : 4352
 * Pw   : "" 
 */

public class PjlinkApp {
	private String IP;
	private String Password;
	private int    Port;
	
	public void pjlinkSetting(String projectorIp, String sPort, String ProjectorPw){
		IP      = projectorIp;
		Port    = Integer.parseInt(sPort);
		Password= ProjectorPw;
	}
	
	public JLinkDataRecv projectorOn(){
		return this.sendRecvData(new JLinkDataSend("POWR","1"));			
	}
	
	public JLinkDataRecv projectorOff(){
		return this.sendRecvData(new JLinkDataSend("POWR","0"));			
	}
	
	public String projectorHours(){
		JLinkDataRecv recv=this.sendRecvData(new JLinkDataSend("LAMP","?"));
		if(recv==null){System.out.println("recv null");return null;}
		if(recv.arg==null){System.out.println("arg null");return null;}
		String[] lamphours=recv.arg.split(" ");
		
		System.out.println("lamp testing");
		return lamphours[0];
	}
	
	public String projectorName(){
		JLinkDataRecv recv=this.sendRecvData(new JLinkDataSend("NAME","?"));
		if(recv==null)return null;
		return recv.arg;
	}
	
	private String readSocketData(BufferedReader in){
		try{
			return in.readLine();
		} catch (IOException e){
			System.out.println("Read failed");
		    System.exit(1);
		}
		return null;
	}
	
	private JLinkDataRecv sendRecvData(JLinkDataSend dataOut){
		PrintWriter out=null;
		BufferedReader in=null;
		Socket PJsock = null;			
		JLinkDataRecv recvd=null;
		String[] Data=null;
		
		try{
			PJsock=new Socket();
			PJsock.bind(null);
			PJsock.connect(new InetSocketAddress(IP, Port), 2000); //4352
			out = new PrintWriter(PJsock.getOutputStream(),true);
		    in = new BufferedReader(new InputStreamReader(PJsock.getInputStream()));
		} catch (UnknownHostException e) {
	     System.out.println("Unknown host: "+IP);
	     return null;
	   } catch  (IOException e) {
	     System.out.println("Cannot connect to ip: "+IP);
	     return null;
	   }
		
	   Data=readSocketData(in).split(" ");
	   if(!Data[0].equals("PJLINK")){
		   System.out.println("projector did not send out magic PJLINK");
	   }else{
		   if(Data[1].equals("1")){
			   System.out.println("projector uses secured link");
			   String secureComp= new String(Data[2]+this.Password);
			   try{
				   MessageDigest md5 = MessageDigest.getInstance("MD5");
				   md5.reset();
				   md5.update(secureComp.getBytes());
				   byte[] digested=md5.digest();
				   String tmp = "";
				   String res = "";
				   
				   for (int i = 0; i < digested.length; i++) {
					   tmp = (Integer.toHexString(0xFF & digested[i]));
					   if (tmp.length() == 1) {
						   res += "0" + tmp;
					   } else {
						   res += tmp;
					   }
				   }
					System.out.println("adding secured MD5 digest: "+res);
					dataOut.AddSecurityData(res);
			   }catch(NoSuchAlgorithmException nsae){					   
				   System.out.println("could not find MD5 digest");
			   }
			   
		   }
		   System.out.println("sending to beamer: "+dataOut.outData);
		   out.write(dataOut.outData.toCharArray());
		   out.write(0x0d);
		   out.flush();
			//Receive text from server
		   String dataIn=readSocketData(in);
		   System.out.println("received from beamer: "+dataIn);
		   if(dataIn!=null){
			   recvd=new JLinkDataRecv(dataIn);
		   }else{
			   System.out.println("received a NULL string from beamer!");
		   }
		   
	   }
		System.out.println("closing socket");
		try {
			PJsock.close();
		} catch (IOException e) {
			System.out.println("failure on socket close");
			e.printStackTrace();
		}
		return recvd;
	}		

	
	class JLinkDataRecv{
		public String cmd;
		public String arg;
		public JLinkDataRecv(String data){
			cmd=data.substring(2,4);
			arg=data.substring(7);
		}
	}
	class JLinkDataSend{
		public String outData;
		public JLinkDataSend(String cmd,String arg){
			outData="%1"+cmd+" "+arg;			
		}
		public void AddSecurityData(String addData){
			outData=addData+outData;
		}
	}
	
	
}
